package com.smalls.redshoes.ui.widget;

import android.content.Context;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.GridView;
import android.widget.RelativeLayout;

import com.smalls.newvideotwo.mvp.bean.StreamInfo;
import com.smalls.redshoes.R;

import java.util.ArrayList;

import cn.bingoogolapple.androidcommon.adapter.BGAAdapterViewAdapter;
import cn.bingoogolapple.androidcommon.adapter.BGAViewHolderHelper;

public class MovieDetailSelectVodLayout extends RelativeLayout {
    private GridViewAdapter mAdapter;
    private Button mBackBtn;
    private GridView mGridView;
    private ArrayList<StreamInfo> mStreamingModels;
    private OnSelectVodClickedListener onSelectVodClickedListener;

    interface OnSelectVodClickedListener {
        void onSelectVodClicked(int i);
    }

    class GridViewAdapter extends BGAAdapterViewAdapter<StreamInfo> {
        public GridViewAdapter(Context context) {
            super(context, R.layout.item_select);
        }

        protected void fillData(BGAViewHolderHelper bgaViewHolderHelper, int i, StreamInfo streamingModel) {
            if (streamingModel != null) {
                if (!TextUtils.isEmpty(streamingModel.getSubtitle())) {
                    bgaViewHolderHelper.setText(R.id.tv_name, streamingModel.getSubtitle());
                }
                bgaViewHolderHelper.setText(R.id.tv_index, streamingModel.getSequence() + "");
            }
        }
    }

    public OnSelectVodClickedListener getOnSelectVodClickedListener() {
        return this.onSelectVodClickedListener;
    }

    public void setOnSelectVodClickedListener(OnSelectVodClickedListener onSelectVodClickedListener) {
        this.onSelectVodClickedListener = onSelectVodClickedListener;
    }

    public MovieDetailSelectVodLayout(Context context) {
        this(context, null);
    }

    public MovieDetailSelectVodLayout(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public MovieDetailSelectVodLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    private void init(Context context) {
        View mRootView = LayoutInflater.from(context).inflate(R.layout.layout_movie_detail_select_vod, this, true);
        this.mGridView = (GridView) mRootView.findViewById(R.id.gv_container);
        this.mBackBtn = (Button) mRootView.findViewById(R.id.btn_back);
        this.mBackBtn.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                MovieDetailSelectVodLayout.this.setVisibility(INVISIBLE);
            }
        });
        this.mAdapter = new GridViewAdapter(context);
        this.mGridView.setAdapter(this.mAdapter);
        this.mGridView.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
                if (MovieDetailSelectVodLayout.this.onSelectVodClickedListener != null) {
                    MovieDetailSelectVodLayout.this.onSelectVodClickedListener.onSelectVodClicked(position);
                }
            }
        });
    }

    public void setDates(ArrayList<StreamInfo> streamingModels) {
        this.mStreamingModels = streamingModels;
        if (this.mAdapter != null) {
            this.mAdapter.setData(this.mStreamingModels);
        }
    }

    public void setVisibility(int visibility) {
        super.setVisibility(visibility);
        if (visibility != 0) {
            return;
        }
        if (this.mAdapter.getCount() > 0) {
            this.mGridView.setFocusable(true);
            this.mGridView.requestFocus();
            return;
        }
        this.mBackBtn.requestFocus();
    }
}
